
using System;
using System.Collections.Generic;

using Grr.Web;
using Grr.Models;

using Castle.MonoRail.Framework;
using Castle.MonoRail.Framework.Views.Aspx;

public partial class Views_Grurrah_tag : GrurrahViewPage {

    private static readonly int MaxResults = 10;
    SearchResult result;

    protected override string TabName
    {
        get { return "explore"; }
    }
    protected override void SetProperties ()
    {
        Tag = (string) PropertyBag["Tag"];
        result = (SearchResult) PropertyBag["TagResult"];

        NextButton.Visible = Result.MoreMatchesAvailable;
    }

    public string Tag
    {
        get {
            object o = ViewState["Tag"];
            if (o == null)
                return String.Empty;
            return (string)o;
        }
        set
        {
            ViewState["Tag"] = value;
        }
    }

    public int CurrentResultsIndex
    {
        get
        {
            object o = ViewState["CurrentResultsIndex"];
            if (o == null)
                return 0;
            return (int)o;
        }
        set
        {
            ViewState["CurrentResultsIndex"] = value;
        }
    }

    public SearchResult Result
    {
        get
        {
            if (result == null)
                return SearchResult.Empty;
            return result;
        }
    }
    protected void NextButton_Click(object sender, EventArgs e)
    {
        result = Grr.Models.Tag.GetGrurrahsForTag (Tag, MaxResults, CurrentResultsIndex + MaxResults);
        CurrentResultsIndex += MaxResults;
        ResultsPanel.ForceAnUpdate();

        PrevButton.Visible = true;
        NextButton.Visible = result.MoreMatchesAvailable;
    }
    protected void PrevButton_Click(object sender, EventArgs e)
    {
        result = Grr.Models.Tag.GetGrurrahsForTag (Tag, MaxResults, CurrentResultsIndex - MaxResults);
        CurrentResultsIndex -= MaxResults;
        ResultsPanel.ForceAnUpdate();

        NextButton.Visible = true;
        PrevButton.Visible = CurrentResultsIndex != 0;
    }
}
